<?php

namespace App\Http\Resources\nursery;

use App\Models\nursery\MajorOrder;
use Illuminate\Http\Resources\Json\Resource;
use SimpleSoftwareIO\QrCode\Facades\QrCode;

class OrderResource extends Resource
{

    public function __construct(MajorOrder $resource)
    {
        $this->resource = $resource;
    }

    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {

        $uri = sprintf("custom=/v1/nursery/signFor=%d",$this->resource->id);
        $path = public_path("/code/".config('app.version').'-nursery-'.$this->resource->id.".png");
        if (!is_file($path)){
            QrCode::format('png')->size(600,600)->generate($uri,$path);
        }

        $url = url("/code/".config('app.version').'-nursery-'.$this->resource->id.".png");

        return [
            'id' => $this->resource->id,
            'nu' => $this->resource->nu,
            'real_name' => $this->resource->check->real_name??'',
            'job_age' => $this->resource->check->job_age??0,
            'header_img' => $this->resource->major_user->header_img,
            'phone' => $this->resource->major_user->phone,
            'major' => $this->resource->major->name,
            'evaluate_tags' => $this->resource->major->evaluate_tags,
            'price' => $this->resource->price,
            'status' => $this->resource->status,
            'address' => $this->resource->address,
            'service_at' => $this->resource->service_at,
            'created_at' => $this->resource->created_at->toDateTimeString(),
            'status_text' => $this->resource->status_text,
            'code_url' => $url,
        ];
    }
}
